package com.jthinker.web.controller.admin;

import com.jthinker.bean.vo.QueryResult;
import com.jthinker.bean.vo.SearchFilter;
import com.jthinker.service.MenuService;
import com.jthinker.web.controller.common.BaseController;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chen.gang on 15-2-22.
 */
@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController
{
    @Resource
    private MenuService menuService;

    @RequestMapping
    public String view(Model model)
    {
        List<Map<String, Object>> menus = menuService.findAll();
        model.addAttribute("menus", menus);
        return "menu/menuList";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> menuList(HttpServletRequest request)
    {
        SearchFilter filter = getSearchFilter(request);
        QueryResult<Map<String, Object>> queryResult = menuService.query(filter);
        return getSuccessMessage(queryResult);
    }

    @RequestMapping("/newMenu")
    public String newMenu(HttpServletRequest request, @RequestParam(value = "id", required = false) String id)
    {
        if (StringUtils.isNotBlank(id))
        {

        }

        request.setAttribute("menus", menuService.findAll());
        return "menu/menuView";
    }

    @RequestMapping(value = "/menuAdd", method = RequestMethod.POST)
    public String menuAdd(HttpServletRequest request)
    {
        String name = request.getParameter("name");
        String type = request.getParameter("type");
        String url = request.getParameter("url");
        String parentId = request.getParameter("parentId");
        String permission = request.getParameter("permission");
        String available = request.getParameter("available");

        List<String> errors = new ArrayList<String>();

        if (StringUtils.isBlank(name))
        {
            errors.add("菜单名不能为空!");
        }

        if (StringUtils.isBlank(type))
        {
            errors.add("菜单类型不能为空!");
        }

        if (StringUtils.isBlank(url))
        {
            errors.add("菜单链接不能为空!");
        }

        if (CollectionUtils.isNotEmpty(errors))
        {
            request.setAttribute("errors", errors);
            return "forward:/admin/menu/newMenu";
        }

        Map<String, Object> record = new HashMap<String, Object>();
        record.put("name", name);
        record.put("parent_id", parentId);
        record.put("type", type);
        record.put("url", url);
        record.put("permission", permission);
        record.put("available", available);

        try
        {
            menuService.create(record);
        }
        catch (Exception e)
        {
            return "forward:/admin/menu/newMenu";
        }
        return "redirect:/admin/menu";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam(value = "id", required = false) String id)
    {
        if (isEmpty(id))
        {
            return getFailMessage("请选择行!");
        }

        try
        {
            menuService.delete(id);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return getFailMessage("删除失败!");
        }

        return getSuccessMessage("删除成功!");
    }

}
